layui.use(['jquery', 'common', 'table', 'form', 'layer', 'util', 'echartsTheme', 'laytpl'], function() {
	var $ = layui.jquery;
	var common = layui.common;
	var table = layui.table;
	var form = layui.form;
	var layer = layui.layer;
	var util = layui.util;
	var echartsTheme = layui.echartsTheme;
	var laytpl = layui.laytpl;

	var statisticsTpl = $('#statisticsTpl').html();
	// 历史羁绊走势图表
	var chart = echarts.init($('#chart').get(0), echartsTheme);
	var refreshData = function() {
		$.getJSON('getChartData', {id: common.getUrlParam('id')}, function(data) {
			if (data.state == 'ok') {
				chart.setOption({
					tooltip: {
						trigger: 'axis',
						formatter: function(params) {
							params = params[0];
							return util.toDateString(params.value[0], 'yyyy-MM-dd') + ' : ' + params.value[1];
						}
					},
					xAxis: {
						type: 'time',
						splitLine: {
							show: false
						},
						axisLabel: {
							formatter: function(value) {
								return util.toDateString(value, 'yyyy-MM-dd');
							}
						}
					},
					yAxis: {
						type: 'value',
						splitLine: {
							show: false
						}
					},
					dataZoom: [{
						show: true
					}, {
						type: 'inside'
					}],
					series: [{
						name: '羁绊值',
						data: data.data,
						type: 'line',
						markLine: {
							symbol: 'none',
							data: data.markLineData
						}
					}]
				});
				laytpl(statisticsTpl).render(data.statistics, function(html) {
					$('#statistics').html(html);
				});
			}
		});
	}
	refreshData();
	window.onresize = chart.resize;
	table.render({
		elem: '#table',
		url: 'tableData',
		page: true,
		toolbar: '#tableToolbar',
		defaultToolbar: [],
		skin: 'line',
		request: {
			pageName: 'current',
			limitName: 'size'
		},
		cols: [[
			{type: 'checkbox'},
			{field: 'level', title: '羁绊等级', sort: true},
			{field: 'value', title: '羁绊值', sort: true},
			{field: 'date', title: '日期', sort: true, templet: common.tableDateTemplet('date', 'yyyy-MM-dd')},
			{title: '操作', minWidth: 168, toolbar: '#rowToolbar'}
		]],
		autoSort: false,
		initSort: {
			field: 'date',
			type: 'asc'
		},
		where: {
			myServantId: common.getUrlParam('id'),
			orderByField: 'date',
			isAsc: true
		}
	});
	table.on('toolbar', function(obj) {
		switch (obj.event) {
		case 'add':
			common.toUrl('form?myServantId=' + common.getUrlParam('id'));
			break;
		case 'delete':
			var data = table.checkStatus('table').data;
			if (data.length) {
				layer.confirm('确定要删除吗？', function(index) {
					var ids = [];
					$.each(data, function() {
						ids.push(this.id);
					});
					common.getJSON('delete', {
						ids: ids,
						myServantId: common.getUrlParam('id')
					}, function(data) {
						layer.msg(data.msg);
						if (data.state == 'ok') {
							// 成功，刷新表格
							table.reload('table', {
								page: {
									curr: 1
								}
							});
							refreshData();
						}
					});
					layer.close(index);
				});
			}
			break;
		default:
			break;
		}
	});
	table.on('tool', function(obj) {
		var data = obj.data;
		console.log(data);

		switch (obj.event) {
		case 'edit':
			common.toUrl('form?id=' + data.id);
			break;
		case 'del':
			layer.confirm('确定要删除吗？', function(index) {
				$.getJSON('delete', {
					ids: data.id,
					myServantId: common.getUrlParam('id')
				}, function(data) {
					layer.msg(data.msg);
					if (data.state == 'ok') {
						// 成功，刷新表格
						table.reload('table', {
							page: {
								curr: 1
							}
						});
						refreshData();
					}
				});
				layer.close(index);
			});
			break;
		default:
			break;
		}
	});
	table.on('sort', function(obj) {
		table.reload('table', {
			initSort: obj,
			where: {
				orderByField: obj.field,
				isAsc: obj.type ? obj.type == 'asc' : null
			}
		})
	});
});